package fun.diaoer.common;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * redis 工具类
 * @author xiaodiannao
 *
 */
@Component
public class RedisUtils {
	
	@Resource(name="redisTemplate")
	private RedisTemplate<String, Object> redisTemplate;
	
	public static boolean set(RedisTemplate<String, ?> redisTemplate,final String key, final String value) {
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                connection.set(serializer.serialize(key), serializer.serialize(value));
                return true;
            }
	    });
		return result;
	}
 
	public static String get(RedisTemplate<String, ?> redisTemplate,final String key){
		String result = redisTemplate.execute(new RedisCallback<String>() {
            public String doInRedis(RedisConnection connection) throws DataAccessException {
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                byte[] value =  connection.get(serializer.serialize(key));
                return serializer.deserialize(value);
            }
	    });
		return result;
	}
 
	public static boolean expire(RedisTemplate<String, ?> redisTemplate,final String key, long expire) {
		return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
	}
 
	public static <T> boolean setList(RedisTemplate<String, ?> redisTemplate,String key, List<T> list) {
		String value = JSON.toJSONString(list);
		return RedisUtils.set(redisTemplate,key,value);
	}
 
	public static <T> List<T> getList(RedisTemplate<String, ?> redisTemplate,String key,Class<T> clz) {
		String json = RedisUtils.get(redisTemplate,key);
		if(json!=null){
			List<T> list = JSON.parseArray(json, clz);
			return list;
		}
		return null;
	}
 
	public static long lpush(RedisTemplate<String, ?> redisTemplate,final String key, Object obj) {
		final String value = JSON.toJSONString(obj);
		long result = redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));
                return count;
            }
	    });
		return result;
	}
 
	public static long rpush(RedisTemplate<String, ?> redisTemplate,final String key, Object obj) {
		final String value = JSON.toJSONString(obj);
		long result = redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                long count = connection.rPush(serializer.serialize(key), serializer.serialize(value));
                return count;
            }
	    });
		return result;
	}
 
	public static String lpop(RedisTemplate<String, ?> redisTemplate,final String key) {
		String result = redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                byte[] res =  connection.lPop(serializer.serialize(key));
                return serializer.deserialize(res);
            }
	    });
		return result;
	}
	
	public static String rpop(RedisTemplate<String, ?> redisTemplate,final String key) {
		String result = redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                byte[] res =  connection.rPop(serializer.serialize(key));
                return serializer.deserialize(res);
            }
	    });
		return result;
	}

	/**
	 * 监听消息通道
	 * @param jedisPubSub - 监听任务
	 * @param channels - 要监听的消息通道
	 */
	/*public static void subscribe(JedisPubSub jedisPubSub, String... channels) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.subscribe(jedisPubSub, channels);
		} finally {
			jedis.close();
		}
	}*/
	
}
